var nav=[]; //导航目录
var option = {
	elem: "#navbread",
}
/**
 * 面包屑操作
 */
var bread = new function(){
	
	this.render = function(opt){
		$.extend(option,opt)
	}
	/**
	 * 重新导航
	 */
	this.start = function(option, pre){
		nav = pre || [];
		nav.push(option);
		biuldBread(nav)
	}
	
	/**
	 * 改变导航
	 */
	this.change = function(option){
		var index = -1;
		for(var i=0;i<nav.length;i++){
					if(nav[i].url == option.url){
						index = i;
						break;
					}	
		}
		if(index == -1){
			nav.push(option);
		}else{
			nav.splice(index+1,nav.length);
		}
		biuldBread(nav);
	}
	
	/**
	 * 点击事件
	 */
	this.click = function(clickEvent){
		$(option.elem).on("click","li.breadcrumb-item>a",function(){
			var url = $(this).data('url');
			var index = -1;
			for(var i=0;i<nav.length;i++){
					if(nav[i].url == url){
						index = i;
						break;
					}	
			}
			var option = nav[index];
			clickEvent(option.url, option.title);
			
		})
	}
	
}
export default bread;
/**
 * 
 * 构建面包屑导航 
 */
function biuldBread(nav) {
	const $nav = $(option.elem);
	var html = ``;
	for (var i = 0; i < nav.length; i++) {
		var item = nav[i];
		if (item.url != undefined) {
			html += `<li class="breadcrumb-item" ><a href="javascript:void(0);" data-url='${item.url}'>${item.title}</a></li>`;
		} else {
			html += `<li class="breadcrumb-item"  aria-current="page">${item.title}</li>`;
		}

	}
	$nav.html(html);
}
